<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$errors = array();

if ($route->POST('action') == 'add')
{
	$company = UV::readInt($route->POST("company"));
	$title = UV::readMQ($route->POST("title"));
	$link = translitIt(strip_tags($title)).".html";
	$i = 1;
	while ($db->getCell("select `link` from `news` where `link` = '$link' limit 1")){
		$link = translitIt(strip_tags($title))."-".($i++).".html";
	}
	$preview = str_replace("\n","<br />",UV::readMQ($route->POST("preview")));
	$text = str_replace("\n","<br />",UV::readMQ($route->POST("text")));
	$date = strtotime(UV::readMQ($route->POST("date")));
	
	$query = "insert into `news` values(NULL,'$company','$title','$link','$preview','$text','$date')";
	
	$update = $db->sendQuery($query);
	if ($update)
		goToPage("/admin/news/edit/".mysql_insert_id());
	else
		$errors[] = "Ошибка добавления записи";
}

loadPage("head.php");
?>
<style type='text/css'>
#ui-datepicker-div { font-size: 11pt; }
#ui-datepicker-div td { padding: 1px; }
</style>
<h1>Добавление новости</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
?>
<form method='POST' id='edit'>
	<input type='hidden' name='action' value='add' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'><label for='company'>Компания</label></td>
			<td width='600px'>
				<select name='company' id='company'><?php
					$companies = $db->getTable("select `company_id`,`name` from `companies` where `head_office` = '0' order by `name`");
					echo select_create($companies,"company_id","name",$route->POST("company"));
				?></select>
			</td>
		</tr>
		<tr>
			<td><label for='title'>Заголовок</label></td>
			<td><input type='text' name='title' id='title' value='<?php echo $route->POST('title'); ?>' /></td>
		</tr>
		<tr>
			<td></td>
			<td><input type='button' id='add_link' value="Добавить ссылку" style="width:150px;height:30px" /></td>
		</tr>
		<tr>
			<td><label for='preview'>Превью</label></td>
			<td><textarea name='preview' id='preview'><?php echo $route->POST('preview'); ?></textarea></td>
		</tr>
		<tr>
			<td><label for='text'>Текст</label></td>
			<td><textarea name='text' id='text'><?php echo $route->POST('text'); ?></textarea></td>
		</tr>
		<tr>
			<td><label for='date'>Дата</label></td>
			<td><input class='short date' type='text' name='date' id='date' value='<?php $route->POST('date'); ?>' /></td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Добавить' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>